package com.kool.kmetrics.metrics;

import com.alibaba.fastjson.JSON;
import com.kool.kmetrics.client.RedisClient;
import com.kool.kmetrics.common.constant.ErrorCode;
import com.kool.kmetrics.common.exception.AppException;
import com.kool.kmetrics.common.util.DateUtil;
import com.kool.kmetrics.common.util.SpringUtil;
import com.kool.kmetrics.common.util.StringUtil;
import com.kool.kmetrics.consumer.vo.SendLog;

import java.text.ParseException;

import static com.kool.kmetrics.constant.MetricsRedisKey.*;

/**
 * 当日各种报文类型的出站报文数、成功数、失败数；
 * 当日用户的各种报文类型的出站报文数、成功数、失败数；
 * 当日客户端的各种报文类型的出站报文数、成功数、失败数；
 * 当日客户端地址的各种报文类型的出站报文数、成功数、失败数；
 *
 * @author luyu
 */
public class SendComputer implements MetricsComputer {
    private RedisClient redisClient = SpringUtil.getBean(RedisClient.class);
    /**
     * key过期时长，单位毫秒
     * 3天
     */
    public static final long TIMEOUT_DURING = 259200000L;

    /**
     * hash结构
     * 参数1：日期
     * hkey：topicName
     * hvalue:空
     */
    public static final String KEY_TODAY_TOPIC = "kmetrics:today_topic:%s";
    /**
     * hash结构
     * 参数1：日期
     * hkey：userName
     * hvalue:空
     */
    public static final String KEY_TODAY_USER = "kmetrics:today_user:%s";
    /**
     * hash结构
     * 参数1：日期
     * hkey：clientId
     * hvalue:空
     */
    public static final String KEY_TODAY_CLIENT_ID = "kmetrics:today_client_id:%s";
    /**
     * hash结构
     * 参数1：日期
     * hkey：remoteAddress
     * hvalue:空
     */
    public static final String KEY_TODAY_REMOTE_ADDRESS = "kmetrics:today_remote_address:%s";

    @Override
    public void compute(String log) {
        SendLog sendLog = JSON.parseObject(log, SendLog.class);
        String time = sendLog.getTimestamp();
        String packetType = sendLog.getPacketType();
        String isSuccess = sendLog.getIsSuccess();
        String userName = sendLog.getUserName();
        String clientId = sendLog.getClientId();
        String remoteAddress = sendLog.getRemoteAddress();
        String topicName = sendLog.getTopicName();

        String now = null;
        try {
            now = Integer.toString(DateUtil.toIntDay(DateUtil.parseDate(time, DateUtil.DateFormatEnum.DEFAULT.getFormat())));
        } catch (ParseException e) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, e, e.getMessage());
        }

        //成功数
        if (isSuccess.equals("1")) {
            //平台维度
            redisClient.inrc(String.format(KEY_TODAY_SEND_SUCCESS, now, packetType, "global", "global"), TIMEOUT_DURING);
            redisClient.inrc(String.format(KEY_TODAY_TOPIC_SEND_SUCCESS, now, topicName, "global", "global"), TIMEOUT_DURING);
            //用户维度
            if (StringUtil.isNotEmpty(userName)) {
                redisClient.inrc(String.format(KEY_TODAY_SEND_SUCCESS, now, packetType, "user_name", userName), TIMEOUT_DURING);
                redisClient.inrc(String.format(KEY_TODAY_TOPIC_SEND_SUCCESS, now, topicName, "user_name", userName), TIMEOUT_DURING);
            }
            //客户端ID维度
            if (StringUtil.isNotEmpty(clientId)) {
                redisClient.inrc(String.format(KEY_TODAY_SEND_SUCCESS, now, packetType, "client_id", clientId), TIMEOUT_DURING);
                redisClient.inrc(String.format(KEY_TODAY_TOPIC_SEND_SUCCESS, now, topicName, "client_id", clientId), TIMEOUT_DURING);
            }
            //客户端地址维度
            if (StringUtil.isNotEmpty(remoteAddress)) {
                redisClient.inrc(String.format(KEY_TODAY_SEND_SUCCESS, now, packetType, "remote_address", remoteAddress), TIMEOUT_DURING);
                redisClient.inrc(String.format(KEY_TODAY_TOPIC_SEND_SUCCESS, now, topicName, "remote_address", remoteAddress), TIMEOUT_DURING);
            }

            //今日topic
            redisClient.hput(String.format(KEY_TODAY_TOPIC, now), topicName, "");
        }
        //失败数
        if (isSuccess.equals("0")) {
            //平台维度
            redisClient.inrc(String.format(KEY_TODAY_SEND_FAIL, now, packetType, "global", "global"), TIMEOUT_DURING);
            //用户维度
            if (StringUtil.isNotEmpty(userName)) {
                redisClient.inrc(String.format(KEY_TODAY_SEND_FAIL, now, packetType, "user_name", userName), TIMEOUT_DURING);
            }
            //客户端ID维度
            if (StringUtil.isNotEmpty(clientId)) {
                redisClient.inrc(String.format(KEY_TODAY_SEND_FAIL, now, packetType, "client_id", clientId), TIMEOUT_DURING);
            }
            //客户端地址维度
            if (StringUtil.isNotEmpty(remoteAddress)) {
                redisClient.inrc(String.format(KEY_TODAY_SEND_FAIL, now, packetType, "remote_address", remoteAddress), TIMEOUT_DURING);
            }
        }
        //总数
        //平台维度
        redisClient.inrc(String.format(KEY_TODAY_SEND_TOTAL, now, packetType, "global", "global"), TIMEOUT_DURING);
        //用户维度
        if (StringUtil.isNotEmpty(userName)) {
            redisClient.inrc(String.format(KEY_TODAY_SEND_TOTAL, now, packetType, "user_name", userName), TIMEOUT_DURING);
            //今日用户
            redisClient.hput(String.format(KEY_TODAY_USER, now), userName, "");
        }
        //客户端ID维度
        if (StringUtil.isNotEmpty(clientId)) {
            redisClient.inrc(String.format(KEY_TODAY_SEND_TOTAL, now, packetType, "client_id", clientId), TIMEOUT_DURING);
            //今日客户
            redisClient.hput(String.format(KEY_TODAY_CLIENT_ID, now), clientId, "");
        }
        //客户端地址维度
        if (StringUtil.isNotEmpty(remoteAddress)) {
            redisClient.inrc(String.format(KEY_TODAY_SEND_TOTAL, now, packetType, "remote_address", remoteAddress), TIMEOUT_DURING);
            //今日客户端地址
            redisClient.hput(String.format(KEY_TODAY_REMOTE_ADDRESS, now), remoteAddress, "");
        }
    }
}
